//
#include "../UdpSocket.h"
#include <stdlib.h>
#include <string.h>
const int BUFFSIZE = 1024;
const int SOCKETTIMEOUT = 2000; //ms

int main(int argc,char *argv[])
{
	UdpSocket client;
	int serPort=0;

	char ipAddr[20];
	memset(ipAddr,0,sizeof(ipAddr));
	if(argc!=3)
	{
		printf("Error: Usage:%s hostname port.\n",argv[0]);
		return -1;
	}
	strcpy(ipAddr,argv[1]);

	if((serPort=atoi(argv[2]))<0)
	{
        printf("Error: Usage:%s hostname port.\n",argv[0]);
        return -1;
    }

	if (client.setServer(ipAddr,serPort))//set udp server's info
	{
		char buffer[BUFFSIZE];
		memset(buffer,0,sizeof(buffer));
		while (1)
		{
			printf("input message:\n");
			scanf("%s",buffer);
			if (strcmp(buffer,"bye")==0)
			{
				printf("quit!\n");
				break;
			}

			//if (-1 != client.sendData(buffer,sizeof(buffer),NULL,0,0)) //block
			if (-1 != client.sendData(buffer,sizeof(buffer),NULL,0,SOCKETTIMEOUT)) //no block
			{
				printf("sending...\n");
				memset(buffer,0,sizeof(buffer));

				//if (-1 != client.recvData(buffer,sizeof(buffer),0)) //use block
				if(0< client.recvData(buffer, sizeof(buffer),SOCKETTIMEOUT)) //use no block 
				{
					printf("the recv data is %s\n",buffer);
				}
			}
			else
			{
				printf("Error: send nothing!\n");
				break;
			}
		}
	}
	client.closeUdpSocket();
	return 1;
}
